package cn.edu.ctbu.datajpa1.webapi;


import cn.edu.ctbu.datajpa1.domain.Student;
import cn.edu.ctbu.datajpa1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;


    /*
    * 读取的url:/webapi/student/list
    * @return
    * */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();

        return students;

    }

    /*
     * 读取的url:/webapi/student/get/id
     * @return
     * */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student students=studentService.getById(id);
        students.setPassword("");

        return students;

    }

   /*
   * 新增学生的方法. /webapi/student/insert
   *@param student
   * @return
   * */
    @PostMapping("/insert")
    public Student insert(Student student){

        Student student1=studentService.insert(student);
        return student1;
    }

    /*
     * 更新学生的方法. /webapi/student/update
     *@param student
     * @return
     * */
    @PutMapping("/update")
    public Student update(Student student){

        Student student1=studentService.update(student);
        return student1;
    }

    /*
     * 新增学生的方法. /webapi/student/delete
     *@param student
     * @return
     * */
    @DeleteMapping("/delete")
    public void delete(Student student){

       studentService.delete(student);
    }

    /*
     * 按姓名查询 /webapi/student/find
     *@param student
     * @return
     * */
    @GetMapping("/find")
    @ResponseBody
    public List<Student> getStudentByName( String name) {
        List<Student> student1=studentService.findAll();

        return student1;
    }

}
